package pers.zff.web.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import pers.zff.web.common.Constants;
import pers.zff.web.common.MessageInfo;
import pers.zff.web.po.UserPo;
import pers.zff.web.service.UserService;
import pers.zff.web.util.ImageAction;
import pers.zff.web.validate.UserValidate;
import pers.zff.web.vo.WebsiteVisit;

@SuppressWarnings("restriction")
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private UserValidate userValidate;
	

	@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody MessageInfo userLogin(HttpServletRequest request, @RequestBody UserPo userpo) {
		MessageInfo messageInfo = MessageInfo.getInstance();
		messageInfo = userValidate.userLoginValidata(userpo, messageInfo);
		if (!messageInfo.hasError()) {
			HttpSession session = request.getSession();
			session.setAttribute("userName", userpo.getUserName());
			session.setAttribute("userId", userpo.getUserId());
			messageInfo.putMessage("登录成功");
			return messageInfo;
		}
		return messageInfo;
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody MessageInfo userRegister(HttpServletRequest request, @RequestBody UserPo userpo) {
		MessageInfo messageInfo = MessageInfo.getInstance();
		messageInfo = userValidate.userRepeatValidate(userpo, messageInfo);
		if (!messageInfo.hasError()) {
			int result = userService.userRegister(userpo);
			if (result == Constants.Integer_one) {
				messageInfo.putMessage("注册成功");
				return messageInfo;
			}
		}
		return messageInfo;
	}

	@RequestMapping(value = "/image")
	public @ResponseBody void image(HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		ImageAction imageAction = new ImageAction();
		String number = null;
		BufferedImage image = null;
		try {
			image = imageAction.execute();
			number = imageAction.getNumber();
			session = request.getSession();
			session.setAttribute("imgCode", number);
		} catch (ImageFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		ServletOutputStream out = response.getOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(image);
		out.flush();
		out.close();
	}

	@RequestMapping(value = "/getimgsession", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, String> getImgSession(HttpSession session, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, String> sessionMap = new HashMap<>();
		String imgCode = session.getAttribute("imgCode").toString();
		sessionMap.put("imgCode", imgCode);
		return sessionMap;
	}

	@RequestMapping(value = "/getusersession", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, String> getUserSession(HttpSession session, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, String> sessionMap = new HashMap<>();
		String userName = session.getAttribute("userName").toString();
		sessionMap.put("userName", userName);
		return sessionMap;
	}

	@RequestMapping(value = "/sendemail", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody MessageInfo sendEmailByupdatePwd(HttpServletRequest request, @RequestBody UserPo userpo) {
		MessageInfo messageInfo = MessageInfo.getInstance();
		Date lastSendRandomTime = new Date();
		Map<String, Object> map = userValidate.sendEmailValidata(userpo, messageInfo);
		messageInfo = (MessageInfo) map.get("messageInfo");
		if (messageInfo.hasError()) {
			return messageInfo;
		}
		return userService.sendEmail(map, lastSendRandomTime, userpo, messageInfo);
	}

	@RequestMapping(value = "/updatepwd", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody MessageInfo updatePwd(HttpServletRequest request, @RequestBody UserPo userpo) {
		MessageInfo messageInfo = MessageInfo.getInstance();
		Map<String, Object> map = userValidate.updatePwdValidata(userpo, messageInfo);
		messageInfo = (MessageInfo) map.get("messageInfo");
		if (messageInfo.hasError()) {
			return messageInfo;
		}
		UserPo user = (UserPo) map.get("userInfo");
		int updatePwd = userService.updatePwd(user.getUserId(), userpo);
		if (updatePwd == Constants.Integer_one) {
			messageInfo.putMessage("修改成功");
			return messageInfo;
		}
		return messageInfo;
	}
	
	@RequestMapping(value = "/saveinfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody MessageInfo saveInfo(HttpServletRequest request, @RequestBody UserPo userpo) {
		MessageInfo messageInfo = MessageInfo.getInstance();
		
		return messageInfo;
	}
	
	
	@RequestMapping(value = "/websitevisit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody MessageInfo saveInfo(HttpServletRequest request, @RequestBody WebsiteVisit websiteVisit) {
		MessageInfo messageInfo = MessageInfo.getInstance();
		return messageInfo; 
	}
	
}
